<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Control Structures</em>
</p>
<p class='vspace'></p><h3>While Loop</h3>
<p>A while loop is a section of code that will loop as long as the conditional argument is true.  While loops are popularly used as forever loops using while(1) or while always true, so that code loops during the entire program execution, theoretically forever.
</p>
<p class='vspace'></p><h3>Hardware Required</h3>
<ul><li>MSP430 LaunchPad
</li><li>(1) digital pushbutton or switch
</li><li>(1) photocell, or analog sensor
</li><li>(2) 10k ohm resistors
</li><li>breadboard
</li></ul><p class='vspace'></p>
<h3>Relevant Groundwork</h3>
<p>Sometimes you want everything in the program to stop while a given condition is true.  You can do this using a <a class='wikilink' href='While.html'><strong>while loop</strong></a>. This example shows how to use a while loop to <a class='wikilink' href='Tutorial_Calibration.html'>calibrate</a> the value of an analog sensor.  
</p>
<p class='vspace'></p><p>In the main loop, the sketch below reads the value of a photoresistor on analog pin A3 and uses it to fade an LED on pin 14.  But while a button attached to digital pin 6 is pressed, the program runs a method called <code>calibrate()</code> that looks for the highest and lowest values of the analog sensor. When you release the button, the sketch continues with the main loop.
</p>
<p class='vspace'></p><p>This technique lets you update the maximum and minimum values for the photoresistor when the lighting conditions change.  
</p>
<p>Connect your analog sensor (e.g. potentiometer, light sensor) on analog input A3 with a 10K ohm resistor to ground. Connect your button to digital pin 6, again with a 10K ohm resistor to ground.  Connect your LED to digital pin 14, with a 220 ohm resistor in series.
</p>
<p class='vspace'></p><h3>Circuit</h3>
<p class='vspace'></p><div><img width='400px' src='../reference/img/WhileLoop_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p><p><h3>Schematic:</h3>
</p>
<p class='vspace'></p><div><img width='500px' src='../reference/img/WhileLoop_schem.png' alt='' title='' /></div>
<p class='vspace'></p><h3>Code Explanation</h3>
<p>None.
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  Conditionals - while statement
 
  This example demonstrates the use of  while() statements.
 
  While the pushbutton is pressed, the sketch runs the calibration routine.
  The  sensor readings during the while loop define the minimum and maximum 
  of expected values from the photo resistor.
 
  This is a variation on the calibrate example.
 
  The circuit:
  * photo resistor connected from +3V to analog in pin A3
  * 10K resistor connected from ground to analog in pin A3
  * LED connected from digital pin 14 to ground through 220 ohm resistor
  * pushbutton attached from pin 6 to +3V
  * 10K resistor attached from pin 6 to ground
 
  created 17 Jan 2009
  by Tom Igoe
  modified 30 Apr 2013
  by Adrian Fernandez
 
  This example code is in the public domain.

*/


// These constants won't change:
const int sensorPin = A3;       // pin that the sensor is attached to
const int ledPin = 14;           // pin that the LED is attached to
const int indicatorLedPin = 2; // pin that the built-in LED is attached to
const int buttonPin = 6;        // pin that the button is attached to


// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value


void setup() {
  // set the LED pins as outputs and the switch pin as input:
  pinMode(indicatorLedPin, OUTPUT);
  pinMode (ledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
}

void loop() {
  // while the button is pressed, take calibration readings:
  while (digitalRead(buttonPin) == HIGH) {
    calibrate(); 
  }
  // signal the end of the calibration period
  digitalWrite(indicatorLedPin, LOW);  

  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

void calibrate() {
  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin, HIGH);
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // record the maximum sensor value
  if (sensorValue > sensorMax) {
    sensorMax = sensorValue;
  }

  // record the minimum sensor value
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;
  }
}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
- run a while loop and then try and exit it cleanly by changing the value of the conditional inside the code

<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='While.html'>while()</a>
</li><li><a class='wikilink' href='DigitalRead.html'>digitalRead()</a>
</li><li><a class='wikilink' href='DigitalWrite.html'>digitalWrite()</a>
</li><li><a class='wikilink' href='AnalogRead.html'>analogRead()</a>
</li><li><a class='wikilink' href='AnalogWrite.html'>analogWrite()</a>
</li><li><a class='wikilink' href='Map.html'>map()</a>
</li><li><a class='wikilink' href='Constrain.html'>constrain()</a>
</li><li><a class='wikilink' href='If.html'>if()</a>
</li><li><a class='wikilink' href='Tutorial_ForLoop.html'>For Loop</a> - Control multiple <span class='wikiword'>LEDs</span> with a For Loop.
</li><li><a class='wikilink' href='Tutorial_SwitchCase.html'>Switch Case</a> - Choose between a number of discrete values in a manner that is the equivalent of using multiples If  statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
</li><li><a class='wikilink' href='Tutorial_Array.html'>Array</a>: a variation on the For Loop example that demonstrates how to use an array.
</li></ul>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
